import React from 'react'
import { useSelector } from 'react-redux'
import type { RootState } from '../store'
import { useLocation, Navigate } from 'react-router-dom'

const Auth = (props: { children: React.ReactNode }) => {
  const location = useLocation()
  const userInfo = useSelector((state: RootState) => state.userStore.userInfo)

  if (!userInfo) return <div>loading...</div>

  if (userInfo?.permissions.find(v => v.path === location.pathname) || location.pathname === '/') {
    return props.children
  } else {
    // return <Navigate to="/403" />
    return props.children
  }
}

export default Auth
